﻿using System;
using System.Net.Sockets;

namespace CS.Beehive
{

    /// <summary>
    /// 连接会话事件参数
    /// </summary>
    public class ChannelEventArgs : EventArgs
    {
        public ChannelEventArgs(ITcpChannel channel)
        {
            Channel = channel;
        }

        /// <summary>
        /// 连接会话
        /// </summary>
        public ITcpChannel Channel { get; internal set; }
    }

    /// <summary>
    /// 连接处理事件
    /// </summary>
    public class ConnectingEventArgs : EventArgs
    {
        public ConnectingEventArgs(TcpServer server)
            : this()
        {
            Server = server;
        }

        public ConnectingEventArgs()
        {
            Cancel = false;
        }

        /// <summary>
        /// 是否可取消
        /// </summary>
        public bool Cancel { get; set; }
        /// <summary>
        /// 连接的服务
        /// </summary>
        public TcpServer Server { get; internal set; }
        /// <summary>
        /// 连接Socket
        /// </summary>
        public Socket ClientSocket { get; internal set; }
    }

    /// <summary>
    /// TcpChannel 断开连接 参数
    /// </summary>
    public class ChannelDisconnectEventArgs : ChannelEventArgs
    {
        public ChannelDisconnectEventArgs(ITcpChannel tcpChannel)
            : base(tcpChannel)
        {
            IsClean = true;
            Delay = 50;
        }

        /// <summary>
        /// 是否清理完成
        /// <remarks>清理完成后才可以真正的断开连接</remarks>
        /// </summary>
        public bool IsClean { get; set; }
        /// <summary>
        /// 延迟
        /// </summary>
        public int Delay { get; set; }

    }

    ///// <summary>
    ///// TcpChannel断开连接（并释放资源）完成
    ///// </summary>
    //public class ChannelDisconnectedEventArgs : ChannelEventArgs
    //{
    //    public ChannelDisconnectedEventArgs(ITcpChannel channel) : base(channel)
    //    {
    //    }
    //}

    /// <summary>
    /// 收到数据事件参数
    /// </summary>
    public class ChannelReceivedEventArgs : ChannelEventArgs
    {
        public ChannelReceivedEventArgs(ITcpChannel channel) : base(channel)
        {
            Data = new Datagram();
        }

        /// <summary>
        /// 转输的数据
        /// </summary>
        public Datagram Data { get; internal set; }

    }

    /// <summary>
    /// 数据发送完成事件
    /// </summary>
    public class ChannelSentEventArgs : ChannelEventArgs
    {
        public ChannelSentEventArgs(ITcpChannel channel) : base(channel)
        {
        }

        public bool IsCompleted { get; internal set; }
    }

    /// <summary>
    /// 异常事件参数
    /// </summary>
    public class TcpExceptionEventArgs : EventArgs
    {
        private readonly Exception _ex;

        public TcpExceptionEventArgs(Exception ex)
        {
            _ex = ex;
        }

        /// <summary>
        /// 异常
        /// </summary>
        public Exception TcpException
        {
            get { return _ex; }
        }

    }

    /// <summary>
    ///带字符串消息的事件参数
    /// <remarks>带一个消息输出</remarks>
    /// </summary>
    [Obsolete("dont use it agin.")]
    public class MessageEventArgs : EventArgs
    {
        private readonly string _message;

        public MessageEventArgs(string message)
        {
            _message = message;
        }
        /// <summary>
        /// 消息
        /// </summary>
        public string Message
        {
            get { return _message; }
        }
    }


    

}